﻿/**
 * 
 * F. Mehmet ARSLAN (c) 2015
 * fmehmetarslan46@gmail.com
 *  
 **/
using ServiceState.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Serialization;

namespace ServiceState.Helper
{
    public class XmlHelper
    {
        public ServicesState testObj
        {
            get
            {
                List<ServiceGroup> serviceState = new List<ServiceGroup>();
                serviceState.Add(new ServiceGroup()
                {
                    Id = "g1",
                    Title = "Production",
                    Services = new List<Service>() { 
                    { new Service() 
                        { 
                            Id = "s1", 
                            Description = "Machine 1", 
                            Method = Method.http, 
                            Url = "#" 
                        } 
                    }, 
                    { new Service() 
                        { 
                            Id = "s2", 
                            Description = "Machine 2", 
                            Method = Method.http, 
                            Url = "#",
                             ChildService = new List<Service>() { 
                    { new Service() 
                        { 
                            Id = "s21", 
                            Description = "Machine 1", 
                            Method = Method.http, 
                            Url = "#" 
                        } 
                    }, 
                    { new Service() 
                        { 
                            Id = "s22", 
                            Description = "Machine 2", 
                            Method = Method.http, 
                            Url = "#" 
                        } 
                    } 
                }
                        } 
                    } 
                }
                });
                serviceState.Add(new ServiceGroup()
                {
                    Id = "g2",
                    Title = "Production",
                    Services = new List<Service>() { 
                    { new Service() 
                        { 
                            Id = "s1", 
                            Description = "Machine 1", 
                            Method = Method.http, 
                            Url = "#" 
                        } 
                    }, 
                    { new Service() 
                        { 
                            Id = "s2", 
                            Description = "Machine 2", 
                            Method = Method.http, 
                            Url = "#",
                             ChildService = new List<Service>() { 
                    { new Service() 
                        { 
                            Id = "s21", 
                            Description = "Machine 1", 
                            Method = Method.http, 
                            Url = "#" 
                        } 
                    }, 
                    { new Service() 
                        { 
                            Id = "s22", 
                            Description = "Machine 2", 
                            Method = Method.http, 
                            Url = "#" 
                        } 
                    } 
                }
                        } 
                    } 
                }
                });
                ServicesState servicesStateObj = new ServicesState() { ServiceGroup = serviceState };
                return servicesStateObj;
            }
        }

        public void WriteXml(ServicesState servicesStateObj, String file)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ServicesState));
            using (StreamWriter sw = new StreamWriter(file))
            {
                xmlSerializer.Serialize(sw, servicesStateObj);
                sw.Close();
                sw.Dispose();
            }
        }
        public ServicesState ReadXml(String file)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ServicesState));
            ServicesState servicesStateObj;
            using (FileStream reader = new FileStream(file, FileMode.Open))
            {
                servicesStateObj = (ServicesState)xmlSerializer.Deserialize(reader);
            }
            return servicesStateObj;
        }
    }
}